//
//  ios_dec_subtitle.m
//  EKK_Player_Lib
//
//  Created by xl on 11-7-21.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//


#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreGraphics/CoreGraphics.h>
#import <CoreText/CoreText.h>
//#import <CoreText/CTStringAttributes.h>

#include "ekk_base/ekk_define.h"
//#import <UIKit/UIKit.h>
#import "ekk_subtitle/ios_dec_subtitle.h"

#if EKK_ENABLE_SUBTITLE

#define MAXFONTSTRSIZE 256

typedef struct ios_subtitle_bitmap_context_st{
	int tav_subtitle_x1;
	int tav_subtitle_y1;
	unsigned long tav_subtitle_rgba;

	char tav_fontstr[MAXFONTSTRSIZE];
	int tav_fontsize;
	CGColorSpaceRef colorSpace;
	CGContextRef context;
	CTFontRef myFont;
	CGColorRef red;
	CFMutableAttributedStringRef attrString;
	CTFramesetterRef framesetter;
	CGMutablePathRef path;
	CTFrameRef frame;
	unsigned char* sbitmap;	
}IOS_SUBTITLE_BITMAP_CONTEXT;

void dec_subtitle_to_bitmap_by_keys_ios(unsigned char** bitmap,char* txt,IOS_DEC_SUBTITLE_HANDLE hd)
{
	
	IOS_SUBTITLE_BITMAP_CONTEXT* p = (IOS_SUBTITLE_BITMAP_CONTEXT*)hd;
	//long long b_ts,e_ts;
	//b_ts = ekk_get_time_in_microsecond();
	ekk_memset(p->sbitmap,0x00,p->tav_subtitle_x1*p->tav_subtitle_y1*4);
	//p->context = (CGContextRef)CGBitmapContextCreate(p->sbitmap, p->tav_subtitle_x1, p->tav_subtitle_y1, 8, 4*(p->tav_subtitle_x1), p->colorSpace, kCGImageAlphaPremultipliedFirst);
	//colorSpace = CGColorSpaceCreateDeviceRGB();
	//sbitmap = ekk_malloc(tav_subtitle_x1*tav_subtitle_y1*4);
	//context = (CGContextRef)CGBitmapContextCreate(sbitmap, tav_subtitle_x1, tav_subtitle_y1, 8, 4*(tav_subtitle_x1), colorSpace, kCGImageAlphaPremultipliedFirst);
	//CGContextSetTextMatrix(context, CGAffineTransformIdentity);	
	// Initialize a rectangular path.
	//CGMutablePathRef path = CGPathCreateMutable();
	//CGRect bounds = CGRectMake(0.0, 0.0, *x, *y);
	//CGPathAddRect(path, NULL, bounds);

	/*
	unsigned char pout[256] ={0};
	int poutlen = 256;
	sd_any_format_to_utf8(txt,sd_strlen(txt),pout,&poutlen);
	
	// Initialize an attributed string.
	CFStringRef string =CFStringCreateWithBytes(NULL,pout,poutlen,kCFStringEncodingUTF8,1);
	 
	//*/
	//NSString* str = 
	//printf("\n dec_subtitle_to_bitmap_by_keys_ios str = %s \n",txt);
	// 1024* 768 or 800 * 600 screen dpi is 96   utf8 is chs 3 bytes
	//multi line txt 
	int x0 =(p->tav_subtitle_x1 - ekk_strlen(txt)*(p->tav_fontsize*72/72)/3)/2 ;
	x0 = x0 <=0 ? 0:x0;
	//int x1 = tav_subtitle_x1 - x0;
	
	p->path = CGPathCreateMutable();
	CGRect bounds = CGRectMake(x0, 0.0, p->tav_subtitle_x1 -x0, p->tav_subtitle_y1);
	CGPathAddRect(p->path, NULL, bounds);
	
	CFStringRef string =CFStringCreateWithBytes(NULL,txt,ekk_strlen(txt),kCFStringEncodingUTF8,1);
	if (string == nil) {
		return;
	}
	p->attrString =
	CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
	CFAttributedStringReplaceString (p->attrString,
									 CFRangeMake(0, CFStringGetLength(p->attrString)), string);
	
	///*
	// Create a color and add it as an attribute to the string.
	/*
	CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
	//float r = (float)((*rgba&0xff000000)>>24)/255;
	//float g = (float)((*rgba&0x00ff0000)>>16)/255;
	//float b = (float)((*rgba&0x0000ff00)>>8)/255;
	//float a = (float)(*rgba&0xff)/255;
	CGFloat components[] ={ 1.0, 0.0, 1.0, 1.0};// { 0.0, 0.0, 1.0, 1.0 };
	CGColorRef red = CGColorCreate(rgbColorSpace, components);
	CGColorSpaceRelease(rgbColorSpace);
	//*/ 
	CFAttributedStringSetAttribute(p->attrString, CFRangeMake(0,CFStringGetLength(string)),
								   kCTForegroundColorAttributeName, p->red);
	
	
	/*
	//char* fontstr = "Helvetica";
	CFStringRef fontName = CFStringCreateWithCString(kCFAllocatorDefault,fonttype,kCFStringEncodingUTF8);;
	
    myFont = CTFontCreateWithName((CFStringRef)fontName, *fontsize, NULL); 
	
	//*/
    CFAttributedStringSetAttribute(p->attrString, CFRangeMake(0, CFStringGetLength(string)), kCTFontAttributeName, p->myFont);	
	//*/ 
	// Create the framesetter with the attributed string.
	
	///*
	p->framesetter =
	CTFramesetterCreateWithAttributedString(p->attrString);
	CFRelease(p->attrString);
	
	// Create the frame and draw it into the graphics context
	p->frame = CTFramesetterCreateFrame(p->framesetter,
												CFRangeMake(0, 0), p->path, NULL);
	CFRelease(p->framesetter);
	//CFRelease(framesetter)
	//*/ 
	//e_ts = ekk_get_time_in_microsecond();
	//printf("\n dec_subtitle_to_bitmap_by_keys_ios **********time = %lld \n",e_ts - b_ts);	
	//b_ts = e_ts;
	CTFrameDraw(p->frame, p->context);
	CFRelease(p->frame);
	CFRelease(string);
	CFRelease(p->path);
	//e_ts = ekk_get_time_in_microsecond();
	//printf("\n dec_subtitle_to_bitmap_by_keys_ios2 **********time = %lld \n",e_ts - b_ts);	
	//b_ts = e_ts;	
	
	
	//CGImageRef theCGImage = CGBitmapContextCreateImage(context);
	//NSData * pixelData = ( NSData * ) CGDataProviderCopyData( CGImageGetDataProvider( theCGImage));
	//unsigned char * pixelBytes = ( unsigned char * ) [ pixelData bytes] ;
	// return pointer to data 
	//ekk_memcpy(sbitmap, pixelBytes, [pixelData length]);
	/*
	for ( int i = 0 ; i < [ pixelData length] ; i += 1 ) { 
		// change accordingly 
		sbitmap[i] = pixelBytes[ i] ;
		
		//bitmap[ i] = 255 ;
		//bitmap[ i+1] = pixelBytes[ i] ;
		//bitmap[ i+ 2 ] = pixelBytes[ i+ 1 ] ;
		//bitmap[ i+ 3 ] = pixelBytes[ i+ 2 ] ;
		
	}
	//*/
	// step through char data 
	//CFRelease(attrString);
	*bitmap = p->sbitmap;
	
	
}
void ios_dec_subtitle_init()
{
	
	/*
	long long b_ts,e_ts;
	b_ts = ekk_get_time_in_microsecond();


	colorSpace = CGColorSpaceCreateDeviceRGB();
	sbitmap = ekk_malloc(tav_subtitle_x1*tav_subtitle_y1*4);
	context = (CGContextRef)CGBitmapContextCreate(sbitmap, tav_subtitle_x1, tav_subtitle_y1, 8, 4*(tav_subtitle_x1), colorSpace, kCGImageAlphaPremultipliedFirst);
	CGContextSetTextMatrix(context, CGAffineTransformIdentity);
	
	path = CGPathCreateMutable();
	CGRect bounds = CGRectMake(0.0, 0.0, tav_subtitle_x1, tav_subtitle_y1);
	CGPathAddRect(path, NULL, bounds);

	
	///*
	attrString = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
	CFStringRef fontName = CFStringCreateWithCString(kCFAllocatorDefault,tav_fontstr,kCFStringEncodingUTF8);;
	
    myFont = CTFontCreateWithName((CFStringRef)fontName, tav_fontsize, NULL); 
	framesetter =
	CTFramesetterCreateWithAttributedString(attrString);
	//CFRelease(attrString);
	
	CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
	float r = (float)((tav_subtitle_rgba&0xff000000)>>24)/255;
	float g = (float)((tav_subtitle_rgba&0x00ff0000)>>16)/255;
	float b = (float)((tav_subtitle_rgba&0x0000ff00)>>8)/255;
	float a = (float)(tav_subtitle_rgba&0xff)/255;
	CGFloat components[] ={ r, g, b, a};// { 0.0, 0.0, 1.0, 1.0 };
	red = CGColorCreate(rgbColorSpace, components);	
	// Create the frame and draw it into the graphics context
	frame = CTFramesetterCreateFrame(framesetter,
												CFRangeMake(0, 0), path, NULL);
	
	e_ts = ekk_get_time_in_microsecond();
	printf("\n ios_dec_subtitle_init **********time = %lld \n",e_ts - b_ts);		
	//CFRelease(framesetter);
	//*/
}


void ios_dec_subtitle_uninit()
{
	/*
	if (sbitmap) {
		ekk_free(sbitmap);
		sbitmap = NULL;
	}
	//CFRelease(framesetter);
	//CFRelease(attrString);
	CFRelease(myFont);
	CFRelease(context);
	//*/
}
IOS_DEC_SUBTITLE_HANDLE ios_dec_subtitle_create()
{
	IOS_SUBTITLE_BITMAP_CONTEXT* p = ekk_malloc(sizeof(IOS_SUBTITLE_BITMAP_CONTEXT));
	ekk_memset(p,0, sizeof(IOS_SUBTITLE_BITMAP_CONTEXT));
	if (p) {
		ekk_sprintf(p->tav_fontstr,"%s","Helvetica");
		p->tav_subtitle_x1 = 400;
		p->tav_subtitle_y1 = 400;
		p->tav_subtitle_rgba =  0xffffffff;
		p->tav_fontsize = 30;
		
		p->colorSpace = CGColorSpaceCreateDeviceRGB();
		p->sbitmap = ekk_malloc(p->tav_subtitle_x1*p->tav_subtitle_y1*4);
		p->context = (CGContextRef)CGBitmapContextCreate(p->sbitmap, p->tav_subtitle_x1, p->tav_subtitle_y1, 8, 4*(p->tav_subtitle_x1), p->colorSpace, kCGImageAlphaNoneSkipLast);
		CGContextSetTextMatrix(p->context, CGAffineTransformIdentity);
		
		p->path = CGPathCreateMutable();
		CGRect bounds = CGRectMake(0.0, 0.0, p->tav_subtitle_x1, p->tav_subtitle_y1);
		CGPathAddRect(p->path, NULL, bounds);
		
		
		///*
		p->attrString = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
		CFStringRef fontName = CFStringCreateWithCString(kCFAllocatorDefault,p->tav_fontstr,kCFStringEncodingUTF8);
		
		p->myFont = CTFontCreateWithName((CFStringRef)fontName, p->tav_fontsize, NULL); 
		p->framesetter =
		CTFramesetterCreateWithAttributedString(p->attrString);
		//CFRelease(attrString);
		
		//CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
		float r = (float)((p->tav_subtitle_rgba&0xff000000)>>24)/255.0;
		float g = (float)((p->tav_subtitle_rgba&0x00ff0000)>>16)/255.0;
		float b = (float)((p->tav_subtitle_rgba&0x0000ff00)>>8)/255.0;
		float a = (float)(p->tav_subtitle_rgba&0xff)/255.0;
		CGFloat components[] ={ 0.0, 1.0, 0.0, 1.0};// { 0.0, 0.0, 1.0, 1.0 };
		p->red = CGColorCreate(p->colorSpace, components);	
		
		
		
		// Create the frame and draw it into the graphics context
		p->frame = CTFramesetterCreateFrame(p->framesetter,
										 CFRangeMake(0, 0), p->path, NULL);
		
	}
	return p;
}

void ios_dec_subtitle_close(IOS_DEC_SUBTITLE_HANDLE hd)
{
	IOS_SUBTITLE_BITMAP_CONTEXT* p = (IOS_SUBTITLE_BITMAP_CONTEXT*)hd;
	if (p) {
		if (p->sbitmap) {
			ekk_free(p->sbitmap);
			p->sbitmap = NULL;
		}
		CFRelease(p->myFont);
		CFRelease(p->context);	
		ekk_free(p);
		p = NULL;
	}	
}
void ios_dec_set_attribute_subtitle(int* x,int* y,unsigned long* rgba,char* fonttype,int* fontsize,IOS_DEC_SUBTITLE_HANDLE hd)
{
	IOS_SUBTITLE_BITMAP_CONTEXT* p = (IOS_SUBTITLE_BITMAP_CONTEXT*)hd;
	if (p) {
		p->tav_subtitle_x1 = *x;
		p->tav_subtitle_y1 =*y;
		p->tav_subtitle_rgba = *rgba;
		if(fonttype)
		{
			ekk_memset(p->tav_fontstr,0,MAXFONTSTRSIZE);
			ekk_memcpy(p->tav_fontstr,fonttype,ekk_strlen(fonttype));
		}

		p->tav_fontsize=*fontsize;
		p->colorSpace = CGColorSpaceCreateDeviceRGB();
		p->sbitmap = ekk_malloc(p->tav_subtitle_x1*p->tav_subtitle_y1*4);
		p->context = (CGContextRef)CGBitmapContextCreate(p->sbitmap, p->tav_subtitle_x1, p->tav_subtitle_y1, 8, 4*(p->tav_subtitle_x1), p->colorSpace, kCGImageAlphaNoneSkipLast);
		CGContextSetTextMatrix(p->context, CGAffineTransformIdentity);
		
		p->path = CGPathCreateMutable();
		CGRect bounds = CGRectMake(0.0, 0.0, p->tav_subtitle_x1, p->tav_subtitle_y1);
		CGPathAddRect(p->path, NULL, bounds);
		
		
		///*
		p->attrString = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
		CFStringRef fontName = CFStringCreateWithCString(kCFAllocatorDefault,p->tav_fontstr,kCFStringEncodingUTF8);;
		
		p->myFont = CTFontCreateWithName((CFStringRef)fontName, p->tav_fontsize, NULL); 
		p->framesetter =
		CTFramesetterCreateWithAttributedString(p->attrString);
		//CFRelease(attrString);
		
		CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
		CGFloat r = (CGFloat)((p->tav_subtitle_rgba&0xff000000)>>24)/255.0;
		CGFloat g = (CGFloat)((p->tav_subtitle_rgba&0x00ff0000)>>16)/255.0;
		CGFloat b = (CGFloat)((p->tav_subtitle_rgba&0x0000ff00)>>8)/255.0;
		CGFloat a = (CGFloat)(p->tav_subtitle_rgba&0xff)/255.0;
		CGFloat components[] ={ r, g, b, a};// { 0.0, 0.0, 1.0, 1.0 };
		p->red = CGColorCreate(rgbColorSpace, components);	
		//p->red =CGColorCreateGenericRGB(r,g,b,a);
		// Create the frame and draw it into the graphics context
		p->frame = CTFramesetterCreateFrame(p->framesetter,
											CFRangeMake(0, 0), p->path, NULL);
		
	}
}


void ios_dec_get_attribute_subtitle(int* x,int* y,unsigned long* rgba,char** fonttype,int* fontsize,IOS_DEC_SUBTITLE_HANDLE hd)
{
	IOS_SUBTITLE_BITMAP_CONTEXT* p = (IOS_SUBTITLE_BITMAP_CONTEXT*)hd;
	if (p) {
		*x = p->tav_subtitle_x1;
		*y = p->tav_subtitle_y1 ;
		*rgba = p->tav_subtitle_rgba;
		*fonttype =p->tav_fontstr;
		*fontsize =p->tav_fontsize;			
	}

}

void ios_dec_subtitle_to_bitmap(unsigned char** bitmap,char* txt,IOS_DEC_SUBTITLE_HANDLE hd)
{
	IOS_SUBTITLE_BITMAP_CONTEXT* p = (IOS_SUBTITLE_BITMAP_CONTEXT*)hd;
	if (p) {	
	dec_subtitle_to_bitmap_by_keys_ios(bitmap,txt,hd);
	}
}
void ios_dec_subtitle_get_empty_bitmap(unsigned char** bitmap,IOS_DEC_SUBTITLE_HANDLE hd)
{
	IOS_SUBTITLE_BITMAP_CONTEXT* p = (IOS_SUBTITLE_BITMAP_CONTEXT*)hd;
	if (p) {	
		ekk_memset(p->sbitmap,0x00,p->tav_subtitle_x1*p->tav_subtitle_y1*4);
		*bitmap = p->sbitmap;
	}	
}

#endif /*#if EKK_ENABLE_SUBTITLE*/
